package com.hongkzh.www.look.enterprise.watch.view.customview;

import android.content.Context;
import android.media.AudioManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import java.util.LinkedHashMap;

import cn.jzvd.JZUtils;
import cn.jzvd.JZVideoPlayer;

/**
 * Created by Nathen on 16/7/30.
 */
public abstract class MyJZVideoPlayer extends JZVideoPlayer {
    LinkedHashMap urlMap;
    int currentUrlMapIndex = 0;

    public MyJZVideoPlayer(Context context) {
        super(context);
    }

    public MyJZVideoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public void setUp(String url, int screen, Object... objects) {
        LinkedHashMap map = new LinkedHashMap();
        map.put(URL_KEY_DEFAULT, url);
        setUp(map, 0, screen, objects);
    }

    public void setUp(LinkedHashMap urlMap, int defaultUrlMapIndex, int screen, Object... objects) {
        super.setUp(urlMap, defaultUrlMapIndex, screen, objects);
        if (this.urlMap != null && !TextUtils.isEmpty(JZUtils.getCurrentUrlFromMap(urlMap, currentUrlMapIndex)) &&
                TextUtils.equals(JZUtils.getCurrentUrlFromMap(this.urlMap, currentUrlMapIndex), JZUtils.getCurrentUrlFromMap(urlMap, currentUrlMapIndex))) {
            return;
        }
        this.urlMap = urlMap;
        this.currentUrlMapIndex = defaultUrlMapIndex;
        this.currentScreen = screen;
        this.objects = objects;
        this.headData = null;
        isVideoRendingStart = false;
        onStateNormal();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        int id = v.getId();
        if (id == cn.jzvd.R.id.surface_container) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.i(TAG, "onTouch surfaceContainer actionDown [" + this.hashCode() + "] ");
                    mTouchingProgressBar = true;

                    mDownX = x;
                    mDownY = y;
                    mChangeVolume = false;
                    mChangePosition = false;
                    mChangeBrightness = false;
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.i(TAG, "onTouch surfaceContainer actionMove [" + this.hashCode() + "] ");
                    float deltaX = x - mDownX;
                    float deltaY = y - mDownY;
                    float absDeltaX = Math.abs(deltaX);
                    float absDeltaY = Math.abs(deltaY);
                    if (currentScreen == SCREEN_WINDOW_FULLSCREEN) {
                        if (!mChangePosition && !mChangeVolume && !mChangeBrightness) {
                            if (absDeltaX > THRESHOLD || absDeltaY > THRESHOLD) {
                                cancelProgressTimer();
                                if (absDeltaX >= THRESHOLD) {
                                    // 全屏模式下的CURRENT_STATE_ERROR状态下,不响应进度拖动事件.
                                    // 否则会因为mediaplayer的状态非法导致App Crash
                                    if (currentState != CURRENT_STATE_ERROR) {
                                        mChangePosition = true;
                                        mGestureDownPosition = getCurrentPositionWhenPlaying();
                                    }
                                } else {
                                    //如果y轴滑动距离超过设置的处理范围，那么进行滑动事件处理
                                    if (mDownX < mScreenWidth * 0.5f) {//左侧改变亮度
                                        mChangeBrightness = true;
                                        WindowManager.LayoutParams lp = JZUtils.getAppCompActivity(getContext()).getWindow().getAttributes();
                                        if (lp.screenBrightness < 0) {
                                            try {
                                                mGestureDownBrightness = Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
                                                Log.i(TAG, "current system brightness: " + mGestureDownBrightness);
                                            } catch (Settings.SettingNotFoundException e) {
                                                e.printStackTrace();
                                            }
                                        } else {
                                            mGestureDownBrightness = lp.screenBrightness * 255;
                                            Log.i(TAG, "current activity brightness: " + mGestureDownBrightness);
                                        }
                                    } else {//右侧改变声音
                                        mChangeVolume = true;
                                        mGestureDownVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                                    }
                                }
                            }
                        }
                    }
//                    if (mChangePosition) {
//                        int totalTimeDuration = getDuration();
//                        mSeekTimePosition = (int) (mGestureDownPosition + deltaX * totalTimeDuration / mScreenWidth);
//                        if (mSeekTimePosition > totalTimeDuration)
//                            mSeekTimePosition = totalTimeDuration;
//                        String seekTime = JZUtils.stringForTime(mSeekTimePosition);
//                        String totalTime = JZUtils.stringForTime(totalTimeDuration);
//
//                        showProgressDialog(deltaX, seekTime, mSeekTimePosition, totalTime, totalTimeDuration);
//                    }
//                    if (mChangeVolume) {
//                        deltaY = -deltaY;
//                        int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//                        int deltaV = (int) (max * deltaY * 3 / mScreenHeight);
//                        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mGestureDownVolume + deltaV, 0);
//                        //dialog中显示百分比
//                        int volumePercent = (int) (mGestureDownVolume * 100 / max + deltaY * 3 * 100 / mScreenHeight);
//                        showVolumeDialog(-deltaY, volumePercent);
//                    }
//
//                    if (mChangeBrightness) {
//                        deltaY = -deltaY;
//                        int deltaV = (int) (255 * deltaY * 3 / mScreenHeight);
//                        WindowManager.LayoutParams params = JZUtils.getAppCompActivity(getContext()).getWindow().getAttributes();
//                        if (((mGestureDownBrightness + deltaV) / 255) >= 1) {//这和声音有区别，必须自己过滤一下负值
//                            params.screenBrightness = 1;
//                        } else if (((mGestureDownBrightness + deltaV) / 255) <= 0) {
//                            params.screenBrightness = 0.01f;
//                        } else {
//                            params.screenBrightness = (mGestureDownBrightness + deltaV) / 255;
//                        }
//                        JZUtils.getAppCompActivity(getContext()).getWindow().setAttributes(params);
//                        //dialog中显示百分比
//                        int brightnessPercent = (int) (mGestureDownBrightness * 100 / 255 + deltaY * 3 * 100 / mScreenHeight);
//                        showBrightnessDialog(brightnessPercent);
////                        mDownY = y;
//                    }
                    break;
                case MotionEvent.ACTION_UP:
                    Log.i(TAG, "onTouch surfaceContainer actionUp [" + this.hashCode() + "] ");
                    mTouchingProgressBar = false;
                    dismissProgressDialog();
                    dismissVolumeDialog();
                    dismissBrightnessDialog();
//                    if (mChangePosition) {
//                        onEvent(JZUserAction.ON_TOUCH_SCREEN_SEEK_POSITION);
//                        JZMediaManager.instance().mediaPlayer.seekTo(mSeekTimePosition);
//                        int duration = getDuration();
//                        int progress = mSeekTimePosition * 100 / (duration == 0 ? 1 : duration);
//                        progressBar.setProgress(progress);
//                    }
//                    if (mChangeVolume) {
//                        onEvent(JZUserAction.ON_TOUCH_SCREEN_SEEK_VOLUME);
//                    }
                    startProgressTimer();
                    break;
            }
        }
        return false;
    }
}
